package com.example.demo.controller;

import com.example.demo.domain.Feedback;
import com.example.demo.service.FeedbackService;
import com.example.demo.util.ParseUsername;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: demo
 * @description: 用户体验反馈的交互类
 * @author: wangze
 * @create: 2020-04-26 17:52
 **/
@RestController
@RequestMapping
@CrossOrigin
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;

    private List<Feedback> feedbackList;

    /**
     * @Description:  获取全部未处理反馈列表
     * @Param:  nowPage：当前页码
     * @Param: pageSize：页码大小
     * @Param: select：选择的搜索种类
     * @Param: searchContent：搜索内容
     * @return: 反馈列表
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('unresolvedFeedback','interaction:feedback:unresolved')")
    @GetMapping(value = "/unresolvedFeedback")
    public String getUnresolvedFeedback(@RequestParam("nowPage")Integer nowPage,@RequestParam("pageSize")Integer pageSize,
                                        @RequestParam("select")String select,@RequestParam("searchContent")String searchContent){
        System.out.println("获取全部未处理反馈列表");
        if(select.equals("workNumber")){
            feedbackList = feedbackService.findUnresolvedFeedbackByWorkNumber(searchContent);
        }else if(select.equals("text")){
            feedbackList = feedbackService.findUnresolvedFeedbackByText(searchContent);
        }else{
            feedbackList = feedbackService.findUnresolvedFeedback(searchContent);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",feedbackList.size());
        List<Feedback> feedback = feedbackList.subList((nowPage - 1) * pageSize,Math.min( nowPage * pageSize,feedbackList.size()));
        JSONArray jsonArray = new JSONArray();
        for(Feedback temp : feedback){
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("id",temp.getId());
            jsonObject1.put("workNumber",temp.getWorkNumber());
            jsonObject1.put("text",temp.getText());
            jsonObject1.put("type",temp.getType());
            jsonObject1.put("state",temp.getState());
            jsonObject1.put("picture",temp.getPicture());
            Date date = temp.getDate();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = dateFormat.format(date);
            jsonObject1.put("date",time);
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("feedback",jsonArray);
        return  jsonObject.toString();
    }

    /**
     * @Description:  获取全部已解决反馈列表
     * @Param:  nowPage：当前页码
     * @Param: pageSize：页码大小
     * @Param: select：选择的搜索种类
     * @Param: searchContent：搜索内容
     * @return: 反馈列表
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('unresolvedFeedback','interaction:feedback:resolved')")
    @GetMapping(value = "/resolvedFeedback")
    public String getResolvedFeedback(@RequestParam("nowPage")Integer nowPage,@RequestParam("pageSize")Integer pageSize,
                                        @RequestParam("select")String select,@RequestParam("searchContent")String searchContent){
        if(select.equals("workNumber")){
            feedbackList = feedbackService.findResolvedFeedbackByWorkNumber(searchContent);
        }else if(select.equals("text")){
            feedbackList = feedbackService.findResolvedFeedbackByText(searchContent);
        }else{
            feedbackList = feedbackService.findResolvedFeedback(searchContent);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",feedbackList.size());
        List<Feedback> feedback = feedbackList.subList((nowPage - 1) * pageSize,Math.min( nowPage * pageSize,feedbackList.size()));
        JSONArray jsonArray = new JSONArray();
        for(Feedback temp : feedback){
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("id",temp.getId());
            jsonObject1.put("workNumber",temp.getWorkNumber());
            jsonObject1.put("text",temp.getText());
            jsonObject1.put("type",temp.getType());
            jsonObject1.put("state",temp.getState());
            jsonObject1.put("picture",temp.getPicture());
            Date date = temp.getDate();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = dateFormat.format(date);
            jsonObject1.put("date",time);
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("feedback",jsonArray);
        return  jsonObject.toString();
    }


    /**
    * @Description: 添加反馈(小程序端)
    * @Param:  feedback: 用户反馈
    * @return:  反馈实体
    * @Author: wangze
    */
    @PreAuthorize("hasPermission('users','users:permissions:basic')")
    @PostMapping(value = "/feedback")
    public Feedback addExperienceFeedback(Feedback feedback){

        return feedbackService.addFeedback(feedback);
    }

    /**
     * @Description:  删除反馈
     * @Param:  id: 反馈ID
     * @return:  无
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('unresolvedFeedback','interaction:feedback:delete')")
    @DeleteMapping(value = "/feedback")
    public void deleteFeedback(@RequestParam("id")Integer id){
        feedbackService.deleteFeedback(id);
    }

    /**
     * @Description: 设置反馈状态为已解决
     * @Param:  id: 反馈ID
     * @return:  反馈实体
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('unresolvedFeedback','interaction:feedback:solve')")
    @GetMapping(value = "/reset-feedback")
    public Feedback resetFeedbackState(@RequestParam("id") Integer id){
        return feedbackService.resetFeedbackState(id);
    }

    /**
     * @Description:  获取反馈详情
     * @Param:  id: 反馈ID
     * @return:  反馈实体
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('unresolvedFeedback','interaction:feedback:detail')")
    @GetMapping(value = "/feedback")
    public Feedback getFeedback(@RequestParam("id")Integer id){
        return feedbackService.getFeedback(id);
    }
}
